package com.et114.components.xmlparse;

/**
 * 
 * @author guanhw
 * 
 */
public class XMLUtils {
	private static char	specialCharactersRepresentation[][];
	static {
		specialCharactersRepresentation = new char [ 63 ] [ ];
		specialCharactersRepresentation [ 38 ] = "&amp;".toCharArray ( );
		specialCharactersRepresentation [ 60 ] = "&lt;".toCharArray ( );
		specialCharactersRepresentation [ 62 ] = "&gt;".toCharArray ( );
		specialCharactersRepresentation [ 34 ] = "&#034;".toCharArray ( );
		specialCharactersRepresentation [ 39 ] = "&#039;".toCharArray ( );
	}
	
	public static String writeEscapedXml ( char buffer[] ){
		int length = buffer.length ; 
		StringBuffer sb  = new StringBuffer();  
		int start = 0;
		for ( int i = 0 ; i < length ; i ++ ) {
			char c = buffer [ i ];
			if ( c > '>' )
				continue;
			char escaped[] = specialCharactersRepresentation [ c ];
			if ( escaped == null )
				continue;
			if ( start < i ) 
				for ( int m = start ; m <  ( i - start ) ; m ++ ) sb.append ( buffer[ m ] ) ;
			sb.append ( escaped );
			start = i + 1;
		}
		
		if ( start < length ) {
			for ( int m = start ; m <  length ; m ++ ) {
				sb.append ( buffer[ m ] ) ;
			}
		}
		return sb.toString ( ) ;
	}
	
	
	public static void main( String[] dd ) throws Exception {
		String s = "等等等等" ;
		System.out.println ( s ) ; 
		
		s = writeEscapedXml( s.toCharArray ( ) ) ; 
		System.out.println ( s ) ; 
	}
}
